
/*
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#include <stdio.h>

#include "libmooigraph.h"
#include "lmg.h"

/* complete the connections information */
void
lmg_connect (void)
{
  struct lmgedgel *eptr;
  struct lmgnode *fn;
  struct lmgnode *tn;
  struct lmgedgel *el;

  eptr = lmgedgelist;
  while (eptr)
    {
      fn = eptr->edge->from;
      tn = eptr->edge->to;
      el = lmg_malloc (sizeof (struct lmgedgel));
      el->edge = eptr->edge;
      if (fn->outend)
	{
	  fn->outend->next = el;
	  fn->outend = el;
	}
      else
	{
	  fn->out = el;
	  fn->outend = el;
	}
      el = lmg_malloc (sizeof (struct lmgedgel));
      el->edge = eptr->edge;
      if (tn->inend)
	{
	  tn->inend->next = el;
	  tn->inend = el;
	}
      else
	{
	  tn->in = el;
	  tn->inend = el;
	}
      eptr = eptr->next;
    }

  return;
}

void
lmg_connectclear (void)
{
  struct lmgnodel *nptr;
  struct lmgedgel *eptr;
  struct lmgedgel *eptr2;

  nptr = lmgnodelist;
  while (nptr)
    {
      if (nptr->node->in)
	{
	  eptr = nptr->node->in;
	  while (eptr)
	    {
	      eptr2 = eptr->next;
	      lmg_free (eptr);
	      eptr = eptr2;
	    }
	}
      nptr->node->in = NULL;
      nptr->node->inend = NULL;
      if (nptr->node->out)
	{
	  eptr = nptr->node->out;
	  while (eptr)
	    {
	      eptr2 = eptr->next;
	      lmg_free (eptr);
	      eptr = eptr2;
	    }
	}
      nptr->node->out = NULL;
      nptr->node->outend = NULL;
      nptr = nptr->next;
    }

  return;
}

/* End */
